<template>
	<view>
		<view
			style="background-color: #fff;display: flex;padding:5px 15px;align-items: center;border-top: 10px solid #f5f5f5;border-bottom: 10px solid #f5f5f5;"
			@click="$util.redirectTo('/pages_tool/invoice/invoice-title-add')">
			<text class="icondiy icon-system-add-circle-line" style="color: green;font-size:25px;"></text>
			<view style="padding-left: 5px;font-weight: bold;">添加发票抬头</view>
		</view>

		<view style="padding:0 15px 5px 15px;display: flex;align-items: center;justify-content: space-between;">
			<view class="">
				可选抬头
			</view>
			<view style="font-size: 10px;color: #999;">
				可点击下方抬头名称进行选择
			</view>
		</view>
		<view>
			<view style="display: flex;border-bottom: 1px solid #eee;padding:10px 15px;background-color: #fff;"
				v-for="(item,index) in invoiceTitleList" :key="index">
				<view style="width: 90%;" @click="selected(item)">
					<view style="font-weight: bold;">
						{{item.name}}
					</view>
					<view style="font-size: 12px;color: #666;">
						{{item.invoice_type_cn}}
					</view>
				</view>
				<view style="width: 10%;padding-top: 10px;">
					<text class="icondiy icon-system-edit-2-fill" style="color: #a8a8a8;font-size: 18px;" @click="edit(item)"></text>
					<text class="icondiy icon-system-delete-bin-fill" style="color:#ff4d4f;font-size: 18px;margin: 0 15rpx" @click="del(item)"></text>
				</view>
			</view>
		</view>
		<view style="height: 50px;"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				invoiceTitleList: []
			}
		},
		onShow() {
			this.init();
		},
		methods: {
			init() {
				this.$api.sendRequest({
					url: '/api/member/getInvoiceList',
					data: {},
					success: res => {
						if (res.code == 0) {
							this.invoiceTitleList = res.data;
						}
					}
				})
			},
			selected(item) {
				this.$api.sendRequest({
					url: '/api/member/setDefaultInvoice',
					data: {
						id: item.id
					},
					success: res => {
						if (res.code == 0) {
							let pages = getCurrentPages();
							let prevPage = pages[pages.length - 2]; // 获取上一个页面的实例
							prevPage.setData({
								setDefault:item
							})

							// 返回上一页
							uni.navigateBack({
								delta: 1
							});
						}
					}
				})
			},
			edit(item) {
				uni.navigateTo({
					url: '/pages_tool/invoice/invoice-title-add?id=' + item.id
				})
			},
			del(item) {
				uni.showModal({
					title: '提示',
					content: '确定删除吗？',
					success: (res) => {
						if (res.confirm) {
							this.$api.sendRequest({
								url: '/api/member/delInvoice',
								data: {
									id: item.id
								},
								success: res => {
									if (res.code == 0) {
										uni.showToast({
											title: '删除成功',
											icon: 'success'
										});
										this.init();
									}
								}
							})
						}
					}
				})
			},
		}
	}
</script>

<style>

</style>